package view.visualizations;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Point2D;
import java.util.Iterator;

import prefuse.Constants;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.layout.AxisLabelLayout;
import prefuse.action.layout.AxisLayout;
import prefuse.controls.ToolTipControl;
import prefuse.render.AxisRenderer;
import prefuse.render.Renderer;
import prefuse.render.RendererFactory;
import prefuse.visual.VisualItem;
import prefuse.visual.expression.VisiblePredicate;
import view.ViewManager;
import view.renderers.LineRenderer;
import view.renderers.TeamColorAction;
import data.DataManager;

public class XCLineVis extends XCVis {

	
	private ActionList draw;
	private ActionList update;
	private int originalMax;
	
	public XCLineVis() {
		this.removeGroup("data");
		tupleSet = this.addTable("data", DataManager.getInstance().getTable());
		xAxis = new AxisLayout("data", "Date", Constants.X_AXIS,
				VisiblePredicate.TRUE);
		yAxis = new AxisLayout("data", "Time", Constants.Y_AXIS,
				VisiblePredicate.TRUE);
		
//		originalMax = yAxis.getRangeModel().getMaximum();
		
		AxisLabelLayout x_labels = new AxisLabelLayout("xlab", xAxis);
		AxisLabelLayout y_labels = new AxisLabelLayout("ylab", yAxis);
		draw = new ActionList();
		draw.add(xAxis);
		draw.add(yAxis);
		draw.add(x_labels);
		draw.add(y_labels);
		
		update = new ActionList();
		update.add(xAxis);
		update.add(yAxis);
		update.add(x_labels);
		update.add(y_labels);
		display.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				Point point = e.getPoint();
				
				Iterator iterator = display.getVisualization().items("data");
				while (iterator.hasNext()) {
					VisualItem item = (VisualItem)iterator.next();
					if (point.distance(new Point2D.Double(item.getX(), item.getY())) < 10 ) {
//						System.out.println(item.get("Name"));
						ViewManager.selectRunners((String)item.get("Name"));
						return;
					}
				}
			}
			@Override
			public void mousePressed(MouseEvent e) {
			}
			@Override
			public void mouseExited(MouseEvent e) {
			}
			@Override
			public void mouseEntered(MouseEvent e) {
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});
		setupVis();
	}
//	public void drawSelected() {
//		this.removeGroup("selections");
//		this.addTable("selections", DataManager.getInstance().getTable(), 
//				(Predicate)ExpressionParser.parse(ViewManager.getInstance().getSelectionQuery()));
//		ActionList selectionActions = new ActionList();
//		TeamColorAction color = new TeamColorAction("selections", VisualItem.STROKECOLOR, this, Color.black);
//		selectionActions.add(color);
//		selectionActions.add(new RepaintAction());
//		this.putAction("selctions", selectionActions);
//		this.run("selections");
//	}
	
	public void setupVis() {
//		yAxis.getRangeModel().setMaximum(originalMax);
//		DataColorAction dc = new DataColorAction("data", "School", Constants.NOMINAL, VisualItem.STROKECOLOR);
		TeamColorAction color = new TeamColorAction("data", "School", Constants.NOMINAL, 
				VisualItem.STROKECOLOR, this);
		draw.add(color);
		
		String[] ttNames = {"Name"};
		ToolTipControl ttControl = new ToolTipControl(ttNames);
		display.addControlListener(ttControl);
		
		
		
		this.setRendererFactory(new RendererFactory() {
//			AbstractShapeRenderer sr = new ShapeRenderer(7);
			LineRenderer lineR = new LineRenderer(XCLineVis.this);
			Renderer arY = new AxisRenderer(Constants.FAR_LEFT,
					Constants.CENTER);
			Renderer arX = new AxisRenderer(Constants.CENTER,
					Constants.FAR_BOTTOM);
 
			public Renderer getRenderer(VisualItem item) {
				return item.isInGroup("ylab") ? arY
						: item.isInGroup("xlab") ? arX : lineR;
			}
		});
		
//		DataShapeAction shape = new DataShapeAction("data", "Meet");
//		draw.add(shape);
		draw.add(new RepaintAction());
		this.putAction("draw", draw);
		this.run("draw");

		update.add(new RepaintAction());
		this.putAction("update", update);

		this.repaint();
	}
	

}
